﻿using Gumini.JulaDB.Provider;
using Gumini.JulaDB.Test.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Gumini.JulaDB.Test
{
    /// <summary>
    /// Tests for JulaDbDataReader.
    /// </summary>
    [TestClass]
    public class DataReaderTest
    {
        private static JulaDbConnection _connection;
        private static PersonTestDataCreator _peopleData;

        [ClassInitialize]
        public static void InitializeDatabase(TestContext context)
        {
            _connection = TestUtils.CreateTestDatabase();
            _peopleData = new PersonTestDataCreator(_connection);
        }

        [ClassCleanup]
        public static void CleanupDatabase()
        {
            _connection.Close();
        }

        /// <summary>
        /// A happy day scenario for a simple query on a single table.
        /// </summary>
        [TestMethod]
        public void ValidSelectReader()
        {
            var reader = _connection.ExecuteValidReader("select FirstName, LastName, Age from Person order by age");
            Assert.IsFalse(reader.IsClosed);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("Maria", reader.GetString(0));
            Assert.AreEqual("Kowalska", reader.GetString(1));
            Assert.AreEqual(11.5M, reader.GetDecimal(2));

            int numberOfRows = 1;
            while (reader.Read())
                numberOfRows++;

            Assert.AreEqual((int)_peopleData.TotalPersonCount, numberOfRows);
            Assert.IsFalse(reader.NextResult());

            reader.Close();
            Assert.IsTrue(reader.IsClosed);
        }
    }
}
